package org.tangjl.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OpenFileServlet
 */
@WebServlet("/OpenFileServlet")
public class OpenFileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public OpenFileServlet() {
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			String folder = "/file/designTemplates.xlsx";
			String swfFolder = "/file/swf/";

			folder = request.getSession().getServletContext().getRealPath(folder);
			String swfPath = request.getSession().getServletContext().getRealPath(swfFolder);

//			// 方案一:
//			File swf = org.tangjl.utill.DocumentConverterUtil.conver(folder,
//					swfPath, swfPath, false, false);
			// 方案二:
			File swf = org.tangjl.utill.jacob.DocumentConverterUtil.conver(
					request.getSession().getId(), folder, swfPath, swfPath, false, false);

			// 文件链接
			String finalFilePath = null;
			if (null != swf) {
				finalFilePath = request.getContextPath() + swfFolder + swf.getName();
			}

			request.setAttribute("finalFilePath", finalFilePath);
		} catch (Exception e) {
			e.printStackTrace();
		}
//		request.getRequestDispatcher("filePreview.jsp").forward(request, response);
		request.getRequestDispatcher("filePreview_2.1.jsp").forward(request, response);
	}

}
